import 'package:flutter/material.dart';
import 'package:flutter_app/config/config.dart';
import 'package:flutter_app/routes/routes.dart';
import 'package:flutter_app/scoped_model/auth.dart';
import 'package:flutter_app/utils/scoped_model_helper.dart';

var _textStyle = const TextStyle(color: Colors.white, fontSize: 16);

class MyHead extends StatefulWidget {
  const MyHead({Key? key}) : super(key: key);

  @override
  State<MyHead> createState() => _MyHeadState();
}

class _MyHeadState extends State<MyHead> {
  Widget _notLogin(context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 25, 0, 25),
      height: 100,
      decoration: const BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          const SizedBox(
            width: 10,
          ),
          Container(
            width: 65,
            height: 65,
            margin: const EdgeInsets.only(right: 10),
            child: const CircleAvatar(
              backgroundImage: AssetImage('images/head.jpeg'),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pushNamed(Routes.login);
                    },
                    child: Text(
                      '登录',
                      style: _textStyle,
                    ),
                  ),
                  Text(
                    '/',
                    style: _textStyle,
                  ),
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pushNamed(Routes.register);
                    },
                    child: Text(
                      '注册',
                      style: _textStyle,
                    ),
                  ),
                ],
              ),
              const Text('登录后可以体验更多', style: TextStyle(color: Colors.white)),
            ],
          )
        ],
      ),
    );
  }

  Widget _haveLogin(context) {
    var userInfo = ScopedHelperModel.getModel<AuthModel>(context).userInfo;
    String _username = userInfo.nickname;
    String _avator = userInfo.avatar;
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 25, 0, 25),
      height: 100,
      decoration: const BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          const SizedBox(
            width: 10,
          ),
          Container(
            width: 65,
            height: 65,
            margin: const EdgeInsets.only(right: 10),
            child: CircleAvatar(
              backgroundImage: NetworkImage(Config.baseUrl + _avator),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    _username,
                    style: _textStyle,
                  )
                ],
              ),
              const Text('编辑个人资料', style: TextStyle(color: Colors.white)),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    bool isLogin = ScopedHelperModel.getModel<AuthModel>(context).isLogin;
    return isLogin ? _haveLogin(context) : _notLogin(context);
  }
}
